package com.woniu.controller;

import com.woniu.dao.UserDao;
import com.woniu.pojo.Clazz;
import com.woniu.pojo.PageBean;
import com.woniu.pojo.User;
import com.woniu.service.ClazzService;
import com.woniu.service.LessonService;
import com.woniu.service.StudentService;
import com.woniu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("clazz")
public class ClazzController{
    @Resource
   private ClazzService clazzService;
    @Resource
    private UserService userService;
    @Resource
    private StudentService studentService;
    @Resource
    private LessonService lessonService;

    //教导主任 可以查看所有班级 可以新增班级修改删除班级
    // 班主任  可以查看自己所负责的班级
    // 教师   可以查看自己所负责的班级
    //点击 我的班级 跳转至页面
    //教导主任跳转
    //教导主任 我的班级 展示内容
    //mclazzList   managerclazzList
    //tclazzList teacherclazzList

    @RequestMapping("mclazzList")
    public String mclazzList(Integer uid,Model model){
        List<Clazz> mclazzList= clazzService.findAllClazz();
        model.addAttribute("mclazzList",mclazzList);
        List<User> userList= userService.findAllUser();
        model.addAttribute("userList",userList);
        model.addAttribute("uid",uid);
        return "clazz/mclazzList";
    }
    @RequestMapping("mclazzListDo")
    @ResponseBody
    public Map<String,Object>  mclazzListDo(PageBean pageBean, Clazz clazz){

        Map<String,Object> resultMap=new HashMap<String,Object> ();
        try {
            List<Clazz> mclazzList= clazzService.findClazzByPage(pageBean,clazz);
            List<User> userList= userService.findAllUser();
            resultMap.put("mclazzList",mclazzList);
            resultMap.put("userList",userList);
            resultMap.put("pageBean",pageBean);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
     * 查询所有班级
     * @param
     * @return
     */
    @RequestMapping("clazzDo")
    @ResponseBody
    public Map<String,Object> clazzDo() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Clazz> clazzList = clazzService.findAll();
            map.put("clazzList", clazzList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 查询所有毕业的班级
     * @param
     * @return
     */
    @RequestMapping("clazzbiyeDo")
    @ResponseBody
    public Map<String,Object> clazzbiyeDo() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Clazz> clazzList = clazzService.findbiyeAll();
            map.put("clazzList", clazzList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 获取毕业时间
     * @param
     * @return
     */
    @RequestMapping("beyetimeDo")
    @ResponseBody
    public Map<String,Object> beyetimeDo() {
        Map<String, Object> map = new HashMap<>();
        try {
            Clazz clazz = clazzService.beyetimeDo();
            map.put("clazz", clazz);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }



        //新增班级
    @RequestMapping("addClazzDo")
    @ResponseBody
    public Map<String,Object> addClazzDo(Clazz clazz){
        Map<String,Object> resultMap=new HashMap<String,Object> ();
        List<Clazz> clazzList=clazzService.findAllClazz();
        List<Clazz> clazzList1=new ArrayList<>();
        try{
            for (Clazz clazz1 : clazzList) {
                if(clazz.getCname().equals(clazz1.getCname())){
                   clazzList1.add(clazz1);
                }
            }
            if(clazzList1.size()==0){
                clazzService.addClazz(clazz);
                resultMap.put("message",null);
                resultMap.put("result",true);
            }else {resultMap.put("message","班级名重复");}
       }catch (Exception e){
            resultMap.put("message",null);
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //删除班级
    @RequestMapping("deleteClazzByCidDo")
    @ResponseBody
    public Map<String,Object> deleteClazzByCidDo(Integer cid){
        Map<String,Object> resultMap=new HashMap<String,Object> ();
        studentService.delStudentByCid(cid);
//        lessonService.deleteLessonByCid(cid);
        clazzService.deleteClazzByCid(cid);
        resultMap.put("result",true);
        return  resultMap;
    }
    //展示要修改的班级内容
    @RequestMapping("toUpdateByCidDo")
    @ResponseBody
    public Map<String,Object> toUpdateByCidDo(Integer cid,Integer uid){
        Map<String,Object> resultMap=new HashMap<String,Object> ();

        Clazz clazz=clazzService.toUpdateByCid(cid);
        List<User> userList= userService.findAllUser();
        resultMap.put("userList",userList);
        resultMap.put("clazz",clazz);

        return resultMap;
    }
    //修改
    @RequestMapping("updateByCidDo")
    @ResponseBody
    public   Map<String,Object> updateByCidDo(Clazz clazz){
        Map<String,Object> resultMap=new HashMap<String,Object> ();
        List<Clazz> clazzList=clazzService.findAllClazz();
        List<Clazz> clazzList1=new ArrayList<>();
        try{
            for (Clazz clazz1 : clazzList) {
                if(clazz.getCname().equals(clazz1.getCname())){
                    clazzList1.add(clazz1);
                }
            }
            if(clazzList1.size()==0){
                clazzService.updateByCid(clazz);
                resultMap.put("message",null);
                resultMap.put("result",true);
            }else {resultMap.put("message","班级名重复");}
        }catch (Exception e){
            resultMap.put("message",null);
            resultMap.put("result",false);
        }
        return resultMap;
    }

    //   教师/班主任跳转
    //   班主任/教师 我的班级 展示内容
    @RequestMapping("tclazzList")
    public String tclazzList(Model model){
        List<Clazz> mclazzList= clazzService.findAllClazz();
        model.addAttribute("mclazzList",mclazzList);
        return "clazz/tclazzList";
    }
    @RequestMapping("tclazzListDo")
    @ResponseBody
    public Map<String,Object> tclazzListDo(PageBean pageBean,Clazz clazz,HttpSession session){
        Map<String,Object> resultMap=new HashMap<String,Object> ();
        User user = (User)session.getAttribute("loginUser");
      List<Clazz> tclazzList=  clazzService.tclazzList(pageBean,clazz,user);
        resultMap.put("pageBean",pageBean);
        resultMap.put("tclazzList",tclazzList);
        resultMap.put("result",true);


        return resultMap;
    }

    @RequestMapping("htclazzList")
    public String htclazzList(Model model){
        List<Clazz> mclazzList= clazzService.findAllClazz();
        model.addAttribute("mclazzList",mclazzList);
        return "clazz/htclazzList";
    }

    @RequestMapping("htclazzListDo")
    @ResponseBody
    public Map<String,Object> htclazzListDo(PageBean pageBean,Clazz clazz,HttpSession session){
        Map<String,Object> resultMap=new HashMap<String,Object> ();
        User user = (User)session.getAttribute("loginUser");
        List<Clazz> htclazzList=  clazzService.htclazzList(pageBean,clazz,user);
        resultMap.put("pageBean",pageBean);
        resultMap.put("htclazzList",htclazzList);
        resultMap.put("result",true);


        return resultMap;
    }
}
